package com.sinosoft.sinopay.apiservice.batchpay.service.impl;

import com.sinosoft.sinocloud.platform.common.util.DateUtil;
import com.sinosoft.sinocloud.platform.common.util.PropertiesUtil;
import com.sinosoft.sinopay.apiservice.batchpay.util.PropertiesUtils;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Properties;

/**
 * 【 银行文件名称生成器 】
 *
 * @author chenzhiheng
 * @version V1.0
 * @date 16/12/21
 */
public class BankNameHandler {

    private static HashMap mNameMap = new HashMap();

    private static final Logger mLogger = Logger.getLogger(BankNameHandler.class);

    public BankNameHandler() {
        regName();
    }

    public static void regName() {

        mNameMap.put("0000", "FirmCode_TradeType_YYYYMMDD_BatchOutfitNo_BatchNo_IOTYPE.txt");

        //企业编号（8 位）+费项编号（5位）+代收付标识（1位：1代付，2代收）+企业自定义文件名”
        mNameMap.put("8888", "FirmCode_TradeType_PAYTYPE_YYYYDDMM_BatchOutfitNo.txt");

        mLogger.debug("初始化银行名称规格完毕，名称个数一共：" + mNameMap.size());
    }


    public static String getNameByCode(String aBankCode, String payType,String batchNo) {
        if (mNameMap.size() < 1) {
            regName();
        }
        String tPath = "/config/bankConfig.properties";
        Properties properties = PropertiesUtils.readProperties(tPath);
        String tFileName = ""; //文件名
        String tToday = ""; //日期
        String tReturnFileName = "";
        String tFirmCode = properties.getProperty(aBankCode+"_FIRMCODE") ;//TODO 获取保险公司代码
        String tBatchOutfitNo = properties.getProperty(aBankCode+"_BATCHOUTFITNO");//TODO 获取批次机构号
        String tTradeType = ""; //交易种类
        String tBusiNo = properties.getProperty(aBankCode+"_BUSINO");///企业编号
        String tFizNo = properties.getProperty(aBankCode+"_FIZNO"); //费项

        if(payType != null && !payType.equals("")){
            if("S4".equals(payType)){
                tTradeType = "PCK";
            }else{
                tTradeType = "PKK";
            }
        }

        try {
            if (aBankCode != null && !"".equals(aBankCode)) {
                if ("0000".equals(aBankCode)) {
                    tFileName = (String) mNameMap.get("0000"); // 获取邮储名字模版
                    tReturnFileName = tFileName.replaceAll("FirmCode",tFirmCode).
                            replaceAll("TradeType",tTradeType).
                            replaceAll("YYYYMMDD", tToday).
                            replaceAll("BatchOutfitNo",tBatchOutfitNo).
                            replaceAll("BatchNo", batchNo).
                            replaceAll("IOTYPE", "i");
                }
                if ("8888".equals(aBankCode)) {
                    tFileName = (String) mNameMap.get("8888"); // 获取西安直连名字模版
                    tReturnFileName = tFileName.replaceAll("FirmCode",tFirmCode).
                            replaceAll("BUSINO",tBusiNo).
                            replaceAll("FIZNO", tFizNo).
                            replaceAll("PAYTYPE","2").
                            replaceAll("YYYYMMDD", tToday).
                            replaceAll("BatchOutfitNo", batchNo);
                }
            }
            mLogger.info("生成文件名称模板：" + tFileName);
        } catch (Exception e) {
            e.printStackTrace();
            mLogger.info("银行文件名称生成发生异常，采用默认格式生成文件名字。");
            return null;
        }
        mLogger.info("银行文件名称：" + tReturnFileName);
        return tReturnFileName;
    }

}
